package service

import (
	"context"
	"fmt"
	"go-toolkit/library/conf/paladin"
	"go-toolkit/library/log"

	pb "test1/api"
	"test1/internal/dao"
)

// Service service.
type Service struct {
	ac  *paladin.Map
	dao dao.Dao
}

// New new a service and return.
func New(d dao.Dao) (s *Service, cf func(), err error) {
	s = &Service{
		ac:  &paladin.TOML{},
		dao: d,
	}
	cf = s.Close
	err = paladin.Watch("application.toml", s.ac)
	return
}

// SayHello grpc demo func.
func (s *Service) SayHello(ctx context.Context, req *pb.HelloReq) (reply *pb.EmptyRsp, err error) {
	reply = new(pb.EmptyRsp)
	fmt.Printf("hello %s\n", req.Name)
	return
}

// SayHelloURL bm demo func.
func (s *Service) SayHelloURL(ctx context.Context, req *pb.HelloReq) (reply *pb.HelloRsp, err error) {
	reply = &pb.HelloRsp{
		Content: "hello " + req.Name,
	}
	fmt.Printf("hello url %s\n", req.Name)
	log.Infoc(ctx, "hello url %s", req)
	return
}

// Ping ping the resource.
func (s *Service) Ping(ctx context.Context, e *pb.EmptyReq) (*pb.EmptyRsp, error) {
	return &pb.EmptyRsp{}, s.dao.Ping(ctx)
}

// Close close the resource.
func (s *Service) Close() {
}
